package com.wangsh.userscenter.head.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wangsh.userscenter.common.controller.BaseController;
import com.wangsh.userscenter.common.util.ConstatFinalUtil;
import com.wangsh.userscenter.service.outer.IOuterService;
import com.wangsh.userscenter.service.users.api.IUsersService;

/**
 * 对外公开的类
 * 
 * @author wangshh
 * 
 */
@Controller
@RequestMapping("/outer")
public class OuterCommonController extends BaseController
{
	@Autowired
	private IOuterService outerService ;
	@Autowired
	private IUsersService usersService;
	
	/**
	 * 查询地区树形结构的JSON
	 * @param request
	 */
	@RequestMapping("/regionList.html")
	public String regionList(HttpServletRequest request,HttpServletResponse response)
	{
		JSONObject resultJSON = new JSONObject() ; 
		String json = request.getParameter("json");
		try
		{
			JSONObject reqJSON = (JSONObject) JSON.parse(json);
			
			if(reqJSON != null)
			{
				String version = reqJSON.getString("version");
				if("1".equalsIgnoreCase(version))
				{
					resultJSON = this.outerService.regionList01Service(reqJSON);
				}
			}else
			{
				//上行参数解析失败
				resultJSON.put("code", "1");
				resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
			}
		} catch (Exception e)
		{
			ConstatFinalUtil.SYS_LOG.error("地区解析上行参数失败了;上行参数:{},",json , e);
		}
		
		this.printOut(request, response, resultJSON.toString());
		return null ;
	}
	
	/**
	 * 验证管理员的Ticket
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/verifyAdminsTicket.html")
	public String verifyAdminsTicket(HttpServletRequest request , HttpServletResponse response) throws Exception
	{
		JSONObject resultJSON = new JSONObject();
		String ticket = request.getParameter("ticket");
		if(ticket == null)
		{
			ticket = "" ; 
		}
		
		JSONObject adminsJSON = this.usersService.verifyAdminsTicketService(ticket);
		if(adminsJSON != null)
		{
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("users", adminsJSON);
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
		}else
		{
			/*resultJSON.put("code", "5");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("5"));*/
			String returnUrl = request.getHeader("Referer");
			if(returnUrl == null)
			{
				returnUrl = "" ; 
			}
			returnUrl = URLEncoder.encode(returnUrl, "UTF-8");
			request.setAttribute("returnUrl", returnUrl);
			return "/login"; 
		}
		this.printOut(request, response, resultJSON + "");
		return null ; 
	}
	
	/**
	 * 验证用户的Ticket
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/verifyUsersTicket.html")
	public String verifyUsersTicket(HttpServletRequest request , HttpServletResponse response) throws Exception
	{
		JSONObject resultJSON = new JSONObject();
		String ticket = request.getParameter("ticket");
		if(ticket == null)
		{
			ticket = "" ; 
		}
		
		JSONObject usersJSON = this.usersService.verifyUsersTicketService(ticket);
		if(usersJSON != null)
		{
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("users", usersJSON);
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
		}else
		{
			/*resultJSON.put("code", "5");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("5"));*/
			String returnUrl = request.getHeader("Referer");
			if(returnUrl == null)
			{
				returnUrl = "" ; 
			}
			returnUrl = URLEncoder.encode(returnUrl, "UTF-8");
			request.setAttribute("returnUrl", returnUrl);
			return "/login"; 
		}
		this.printOut(request, response, resultJSON + "");
		return null ; 
	}
}
